/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vaccination;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.PropertyConfigurator;
import utils.DBController;
import utils.DBResultSet;
import utils.Utils;

/**
 *
 * @author PacosHome
 */
public class Dottore_VisualizzaRichiamati extends HttpServlet {
    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Dottore_VisualizzaRichiamati.class); //avvio logger di log4j
    @Override
    public void init() {
        PropertyConfigurator.configure(Dottore_VisualizzaRichiamati.class.getClassLoader().getResource("utils/log4j.properties"));
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        
        String sec = request.getParameter("seconds");
        String message = "";
        int seconds = -1;
        int contatore = 0;
        
        // se è stato inserito un valore valido si prosegue con la procedura di richiamo
        // altrimenti si mostra il form di richiesta dei secondi con un eventuale messaggio d'errore
        if(sec != null){
            if(!Utils.tryParseUInt(sec)) {
                message = "Inserire un valore valido";
            }
            else {
                seconds = Integer.parseInt(sec);
            }
        }
        
        DBController dbController = DBController.getInstance();
        
        try
        {
            Utils.printHeader(request,out,"Visualizza Richiamati");
            
            // form base dei secondi
            if(seconds == -1) {
                
                out.println("            <div class=\"container-fluid-medium\">\n" +
                            "               <div class=\"row-fluid\">\n" +
                            "                    <div class=\"span6\">\n" +
                            "                        <div class=\"well\">\n" +
                            "                            <ul class=\"nav nav-list\">\n" +
                            "                              <li class=\"nav-header\">Vaccini</li>\n" +
                            "                              <li><a href=\"../dottore_proceduraRichiamoPazienti.html\">Procedura Richiamo Pazienti</a></li>\n" +
                            "                              <li><a href=\"../dottore_visualizzaRichiamati.html\">Visualizza Pazienti Richiamati</a></li>\n" +
                            "                              <li class=\"active\"><a href=\"#\">- Lista</a></li>\n" +
                            "                              <li class=\"nav-header\">Utente</li>\n" +
                            "                              <li><a href=\"Home\">Home</a></li>\n" +
                            "                              <li><a href=\"Logout\">Logout</a></li>\n" +
                            "                            </ul>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                    <div class=\"well span6\" style=\"padding:10px;\">\n" +
                            "                       <div>\n" +
                            "                           <form method=\"post\">\n"+                
                            "                               <div class=\"box\">\n");
                if(!message.isEmpty()) {
                out.println("                                   <p id=\"error-message\">" + message + "</p>\n"+
                            "                                   <label>Intervallo in secondi</label><input type=\"text\" name=\"seconds\" autofocus />\n"+
                            "                                   <br>\n" +
                            "                                   <input class=\"btn btn-primary\" type=\"submit\" value=\"Conferma\" />\n"+
                            "                               </div>\n"+
                            "                           </form>\n");
                }
                
                out.println("                       </div>\n" +
                            "                   </div>\n");
                

            } else {
                
                out.println("            <div class=\"container-fluid-large\">\n" +
                            "               <div class=\"row-fluid\">\n" +
                            "                    <div class=\"span4\">\n" +
                            "                        <div class=\"well\">\n" +
                            "                            <ul class=\"nav nav-list\">\n" +
                            "                              <li class=\"nav-header\">Vaccini</li>\n" +
                            "                              <li><a href=\"../dottore_proceduraRichiamoPazienti.html\">Procedura Richiamo Pazienti</a></li>\n" +
                            "                              <li><a href=\"../dottore_visualizzaRichiamati.html\">Visualizza Pazienti Richiamati</a></li>\n" +
                            "                              <li class=\"active\"><a href=\"#\">- Lista</a></li>\n" +
                            "                              <li class=\"nav-header\">Utente</li>\n" +
                            "                              <li><a href=\"Home\">Home</a></li>\n" +
                            "                              <li><a href=\"Logout\">Logout</a></li>\n" +
                            "                            </ul>\n" +
                            "                        </div>\n" +
                            "                    </div>\n" +
                            "                    <div class=\"well span8\" style=\"padding:10px;\">\n" +
                            "                       <div>\n");
                
                // selezione dei pazienti che non hanno richiami negli ultimi X secondi indicati
                DBResultSet dbResult = dbController.getPatientsAfterTime(seconds);
                ResultSet patients = dbResult.getResultSet();
                
                //visualizza lista pazienti
                if(!patients.next()) {
                    out.println("                           <h2>Nessuna registrazione trovata</h2>\n");
                } else {
                    out.println("                           <table class=\"table\">\n" +
                                "                                <tr>\n" +
                                "                                    <td width=\"135\"><h5>Paziente</h5></td>\n" +
                                "                                    <td width=\"120\"><h5>Data di Nascita</h5></td>\n" +
                                "                                    <td width=\"180\"><h5>Data</h5></td>\n" +
                                "                                    <td width=\"135\"><h5>Dottore</h5></td>\n" +
                                "                                </tr>\n");
                    
                    boolean evenline = true;
                    do {
                        if(evenline) {
                            out.print("                         <tr style=\"background:#cccccc;\">\n");
                        } else {
                            out.println("                       <tr>\n");
                        }
                        
                        out.println("                                   <td>\n" +
                                    "                                        <div class=\"accordion-group\">\n" +
                                    "                                            <div class=\"accordion-heading\">\n" +
                                    "                                              <a class=\"accordion-toggle\" data-toggle=\"collapse\" data-parent=\"#accordion2\" href=\"#collapse"+ patients.getString("id") +"\">\n" +
                                    "                                                " + patients.getString("name") + " " + patients.getString("surname") + "\n" +
                                    "                                              </a>\n" +
                                    "                                            </div>\n" +
                                    "                                        </div>\n" +
                                    "                                    </td>\n" +
                                    "                                    <td style=\"padding: 7px;\">" + Utils.formatDate(patients.getTimestamp("birthdate")) + "</td>\n" +
                                    "                                    <td style=\"padding: 7px;\">" + Utils.formatDateTime(patients.getTimestamp("date")) + "</td>\n" +
                                    "                                    <td style=\"padding: 7px;\">" + patients.getString("docname") + " " + patients.getString("docsurname") + "</td>");
                        out.println("                           </tr>");
                        
                        dbResult = dbController.getVaccinations(Integer.parseInt(patients.getString("id")));
                        ResultSet vaccinations = dbResult.getResultSet();
                        if(evenline) {
                            out.print("                         <tr style=\"background:#cccccc;\">\n");
                        } else {
                            out.println("                       <tr>\n");
                        }
                        
                        out.println("                                    <td colspan=\"4\">\n" +                                    
                                    "                                        <div id=\"collapse"+ patients.getString("id") +"\" class=\"accordion-body collapse\">\n" +
                                    "                                            <div class=\"accordion-inner\">\n" +
                                    "                                               <div class=\"well span5\" style=\"padding:10px;\">\n");

                        if(!vaccinations.next()) {
                            out.println("                                           <h4>Nessuna vaccinazione effettuata</h4>\n");
                        } else {
                            out.println("                                           <h4>Ultime Vaccinazioni</h4>\n");
//                            
                            out.println("                                           <table>\n" +
                                        "                                               <tr>\n" +
                                        "                                                   <td width=\"180\">Data</td>\n" +
                                        "                                                   <td width=\"135\">Dottore</td>\n" +
                                        "                                               </tr>\n");
                            do {
                            out.println("                                               <tr>\n" +
                                        "                                                   <td>" + Utils.formatDate(vaccinations.getDate("date")) + "</td>\n" +
                                        "                                                   <td>" + vaccinations.getString("name") + " " + vaccinations.getString("surname") + "</td>\n" +
                                        "                                               </tr>\n");                        
                            } while(vaccinations.next());
                            out.println("                                           </table>\n");
                        }
                        
                        out.println("                                               </div>\n" +
                                    "                                           </div>\n" +
                                    "                                       </div>" +
                                    "                                    </td>\n" +
                                    "                           </tr>\n" +
                                    "                       </div>\n");
                        
                        
                        evenline = !evenline;
                    } while(patients.next());
                    
                    out.println("                      </table\n");
                    
                    out.println("        <!-- Il javascript -->\n" +
                                "        <script src=\"../styles/jquery.js\"></script>\n" +
                                "        <script src=\"../styles/javascript.js\"></script>");
                }
                dbController.releaseConnection(dbResult);
            }
                        
            Utils.printFooter(out);
            
        } catch(Exception e) {
            //stampa tramite log4j il messaggio dell'eccezione e restituisce la pagine di errore
            log.error(e.getMessage());            
            Utils.printHeader(request, out, "Errore!");
            Utils.printError(request, out, e.getMessage());
            Utils.printFooter(out);
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
